//
//  WCSelectHospitalOrWardNVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/6.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNSelectHospitalOrWardVC.h"
//
#import "MJRefresh.h"
//
#import "AccountEntity.h"
#import "WCNConstantsCDStore.h"
#import "WCNetworkAccessManager.h"
//
#import "Macros.h"
#import "Constants.h"

static NSString *cellId = @"UITableViewCell";

@interface WCNSelectHospitalOrWardVC ()

@property (nonatomic) NSInteger selectedRow;

@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSArray *list;

@end

@implementation WCNSelectHospitalOrWardVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _selectedRow = -1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kAppBg;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    self.tableView.rowHeight = 50;
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf refreshList];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.selectMode == WCNSelectVCHospital) {
        self.url = @"hospital/list";
        self.list = [[WCNConstantsCDStore shareStore] getHospitals];
    } else if(self.selectMode == WCNSelectVCWard) {
        self.url = [NSString stringWithFormat:@"hospital/ward/list/%@", self.account.hospitalId];
        self.list = [[WCNConstantsCDStore shareStore] getWardsByhospital:self.account.hospitalId];
    }
    [self initSelectRow];
    
    if ([[WCNetworkAccessManager sharedMg] timeSinceLastUrl:self.url] > 2*kUrlCD && ![self.tableView.header isRefreshing]) {
        [self.tableView.header beginRefreshing];
    }
}

- (void)initSelectRow
{
    NSString *selectedId;
    if (self.selectMode == WCNSelectVCHospital) {
        selectedId = self.account.hospitalId;
    } else if(self.selectMode == WCNSelectVCWard) {
        selectedId = self.account.wardId;
    }
    
    for (int i=0; i<self.list.count; i++) {
        NSString *stringId = [self.list[i] valueForKey:@"stringId"];
        if ([stringId isEqualToString:selectedId]) {
            self.selectedRow = i;
        }
    }
}

- (void)refreshList
{
    [[WCNetworkAccessManager sharedMg] GET:self.url parameters:nil success:^(NSDictionary *data){
        if (self.selectMode == WCNSelectVCHospital) {
            self.list = [[WCNConstantsCDStore shareStore] createHospitals:data[@"data"]];
        } else if(self.selectMode == WCNSelectVCWard) {
            self.list = [[WCNConstantsCDStore shareStore] createWards:data[@"data"] hospital:self.account.hospitalId];
        }
        
        [self initSelectRow];
        
        [self.tableView reloadData];
    } failure:nil netError:nil always:^{
        [self.tableView.header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    [cell setSeparatorInset:UIEdgeInsetsZero];
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if (self.selectedRow == indexPath.row) {
        cell.imageView.image = [UIImage imageNamed:@"Selected"];
    } else {
        cell.imageView.image = [UIImage imageNamed:@"Selected_no"];
    }
    cell.textLabel.text = [self.list[indexPath.row] valueForKey:@"name"];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"Selected"];
    [cell setSelected:NO animated:YES];
    
    self.selectedRow = indexPath.row;
    
    NSString *stringId = [self.list[indexPath.row] valueForKey:@"stringId"];
    NSString *name = [self.list[indexPath.row] valueForKey:@"name"];

    if (self.selectMode == WCNSelectVCHospital) {
        if (![stringId isEqualToString:self.account.hospitalId]) {
            self.account.hospitalId = stringId;
            self.account.hospitalName = name;
            
            self.account.wardId = nil;
            self.account.wardName = nil;
        }
    } else if(self.selectMode == WCNSelectVCWard) {
        if (![stringId isEqualToString:self.account.hospitalId]) {
            self.account.wardId = stringId;
            self.account.wardName = name;
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"Selected_no"];
}

@end
